//
//  ComposeView.m
//  Golf
//
//  Created by 周成龙 on 16/11/3.
//  Copyright © 2016年 ZCL. All rights reserved.
//

#import "ComposeView.h"
#import "ZConstant.h"

#import "ComposeItem.h"

@interface ComposeView ()

@end

@implementation ComposeView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        [self createButtons];
    }
    return self;
}

- (void)createButtons {
    _itemCount = 3;
    
    NSArray *titles = @[@"场地查询", @"约球", @"发布信息"];
    
    for (int i = 0; i < _itemCount; i ++) {
        ComposeItem *btn = [[ComposeItem alloc] init];
        btn.rate = 0.2;
        btn.bounds = CGRectMake(0, 0, 40, 50);
        btn.center = CGPointMake(kScreenWidth*0.5, kScreenHeight-49*0.5);
        
        NSString *title = titles[i];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        btn.titleLabel.font = [UIFont systemFontOfSize:10.f];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        NSString *imageName = [NSString stringWithFormat:@"%@.png", title];
        [btn setImage:[UIImage imageNamed:kGetImageName(imageName)] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:kGetImageName(imageName)] forState:UIControlStateHighlighted];
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.hidden = YES;
        btn.tag = i+1;
        [self addSubview:btn];
    }
}

- (void)btnClick:(ComposeItem *)sender {
    //[[NSNotificationCenter defaultCenter] postNotificationName:@"kNoticeDismissView" object:nil];
    if (_composeBlock) {
        _composeBlock(sender.tag);
    }
}

- (void)addAnimation {
    
    int count = (int)self.subviews.count;
    CGFloat angle = 0.0;
    CGFloat radius = 110;
    angle = (M_PI / (count+1));
    
    CGFloat width = kScreenWidth*0.5;
    for (int i = 0; i < count; i ++) {
        int a = i+1;
        
        CGFloat w = radius*cos(a*angle);
        CGFloat h = radius*sin(a*angle);
        CGFloat x = width + (angle>M_PI_2 ? (-w):w);
        CGFloat y = kScreenHeight - h;
        
        UIButton *btn = (UIButton *)[self viewWithTag:i+1];
        btn.hidden = NO;
        [UIView animateWithDuration:0.35 animations:^{
            btn.center = CGPointMake(x, y);
        } completion:^(BOOL finished) {
            
        }];
    }
}

- (void)showInView:(UIView *)view belowSubview:(UIView *)subView {
    [view insertSubview:self belowSubview:subView];
    
    [UIView animateWithDuration:0.1 animations:^{
        self.alpha = 0.7;
    } completion:^(BOOL finished) {
        [self addAnimation];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kNoticeDismissView" object:nil];
}

- (void)dealloc {
    MyLog(@"%s", __func__);
}

@end
